<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="827px"
  >
    <!-- 标题栏 -->
    <div class="title">
      <div>资源认领</div>
      <i class="iconfont icon-guanbi2fill" @click="dialogVisible = false"></i>
    </div>
    <div style="padding: 0 36px 30px 36px">
      <el-form :model="form" :rules="rules" ref="formRef">
        <!-- 认领资源 -->
        <el-form-item label="认领资源" prop="resource">
          <el-input
            v-model="form.resource"
            disabled
            placeholder="请输入认领资源"
          />
        </el-form-item>

        <!-- 认领人 -->
        <el-form-item label="认领人" prop="claimer">
          <el-input v-model="form.claimer" placeholder="请输入您的姓名" />
        </el-form-item>

        <!-- 认领机构 -->
        <el-form-item label="认领机构" prop="institution">
          <el-input v-model="form.institution" placeholder="请输入您的机构" />
        </el-form-item>

        <!-- 认领内容（单选组） -->
        <el-form-item label="认领内容" prop="claimType">
          <el-radio-group v-model="form.claimType">
            <el-radio label="第一作者" :disabled="claimStatus.firstAuthor"
              >第一作者</el-radio
            >
            <el-radio label="第二作者" :disabled="claimStatus.secondAuthor"
              >第二作者</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <!-- 联系方式 -->
        <el-form-item label="联系方式" prop="contact">
          <el-input
            v-model="form.contact"
            placeholder="请输入您的邮箱和手机号"
          ></el-input>
        </el-form-item>
        <!-- 上传证明材料 -->
        <el-form-item label="上传证明材料" prop="images">
          <div style="width: 100%">
            <el-upload
              action="/api/frontend/file/upload"
              :headers="{ Authorization: token }"
              :limit="1"
              drag
              :on-remove="handleRemove"
              :before-upload="beforeUpload"
              :on-success="handleSuccess"
              :on-exceed="handleExceed"
            >
              <div
                style="
                  display: flex;
                  flex-direction: column;
                  align-items: center;
                "
              >
                <img src="../assets/img/shangc.png" alt="" />
                <div class="z">点击或将文件拖到此处</div>
              </div>
            </el-upload>
          </div>
        </el-form-item>

        <!-- 提交按钮 -->
        <el-form-item>
          <div style="display: flex; justify-content: center">
            <el-button
              style="width: 120px; height: 48px"
              type="primary"
              @click="submitForm"
              >提交</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import service from "@/service/service";
import { cookieGet } from "@/storageCookie";

export default {
  name: "ResourceClaimDialog",
  props: {
    huangitem: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      token: cookieGet("tsgfAuthorization"),
      dialogVisible: false,
      form: {
        resource: "《督练公所与清末军事改革的地方实践——兼谈督抚军权问题》",
        claimer: "",
        institution: "",
        claimType: "第一作者",
        contact: "",
        images: [],
      },
      claimStatus: {
        firstAuthor: false,
        secondAuthor: false,
      },
      rules: {
        resource: [
          { required: true, message: "认领资源不能为空", trigger: "blur" },
        ],
        claimer: [
          { required: true, message: "请输入认领人姓名", trigger: "blur" },
        ],
        institution: [
          { required: true, message: "请输入认领机构", trigger: "blur" },
        ],
        claimType: [
          { required: true, message: "请选择认领内容", trigger: "change" },
        ],
        contact: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              const phoneReg = /^1[3-9]\d{9}$/;
              const emailReg =
                /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
              if (phoneReg.test(value) || emailReg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入有效的手机号或邮箱"));
              }
            },
            trigger: "blur",
          },
        ],
        images: [
          { required: true, message: "请上传证明材料", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    // 文件上传成功处理
    handleSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.form.images = fileList;
        this.$message({
          message: "文件上传成功",
          type: "success",
          duration: 2000,
          showClose: true,
        });
        return true;
      } else {
        this.form.images = [];
        this.$message({
          message: response.msg || "文件上传失败",
          type: "error",
          duration: 3000,
          showClose: true,
        });
        return false;
      }
    },
    handleExceed() {
      this.$message.warning("最多只能上传一个文件");
    },
    // 文件删除处理
    handleRemove(file, fileList) {
      this.form.images = fileList;
      console.log(file, "111");
    },

    // 上传前校验
    beforeUpload() {
      return true;
    },

    // 打开对话框
    openFeedbackDialog() {
      this.dialogVisible = true;
      if (!this.claimStatus.firstAuthor && !this.claimStatus.secondAuthor) {
        this.form.claimType = "第一作者";
      }
    },

    // 提交表单
    async submitForm() {
      try {
        await this.$refs.formRef.validate();

        const response = await service({
          url: "/frontend/resourceAuthor/add",
          method: "post",
          data: {
            // resource: this.form.resource,
            // claimer: this.form.claimer,
            // institution: this.form.institution,
            // claimType: this.form.claimType,
            // contact: this.form.contact,
            // fileIds: this.form.images.map((item) => item.response.data),

  //           "resourceId": 0,
  // "authorId": 0,
  // "unitId": 0,
  // "fieldName": "",
  // "contact": "",
  // "fileId": 0

          },
        });

        if (response.code === 200) {
          this.$message({
            message: "资源认领提交成功！",
            type: "success",
            duration: 3000,
            showClose: true,
          });
          this.dialogVisible = false;
          this.resetForm();
          this.$emit("success"); // 通知父组件提交成功
        } else {
          this.$message({
            message: response.msg || "提交失败",
            type: "error",
            duration: 3000,
            showClose: true,
          });
        }
      } catch (error) {
        if (error === false) {
          this.$message({
            message: "请完善表单信息后提交",
            type: "warning",
            duration: 3000,
            showClose: true,
          });
        } else {
          this.$message({
            message: "提交失败，请稍后重试",
            type: "error",
            duration: 3000,
            showClose: true,
          });
        }
      }
    },

    // 取消表单
    cancelForm() {
      this.$message({
        message: "已取消认领",
        type: "info",
        duration: 2000,
        showClose: true,
      });
      this.dialogVisible = false;
      this.resetForm();
    },

    // 重置表单
    resetForm() {
      this.$refs.formRef.resetFields();
      this.form.images = [];
      if (!this.claimStatus.firstAuthor) {
        this.form.claimType = "第一作者";
      } else if (!this.claimStatus.secondAuthor) {
        this.form.claimType = "第二作者";
      }
    },
  },
};
</script>

<style lang="scss" scoped>
// 在现有样式基础上添加以下样式
::v-deep .el-upload-dragger {
  width: 750px;
  height: 158px;
  background: #f9faff;
  border: 2px dashed #ffff;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;

  &:hover {
    border-color: #409eff;
    background: #f0f7ff;
  }
  .z {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #999999;
  }
  img {
    width: 72px;
    height: 72px;
    margin-top: 27px;
  }

  .upload-icon {
    font-size: 48px;
    color: #c0c4cc;
    margin: 40px 0 16px;
    line-height: 50px;
  }
}

// 文件列表样式优化
::v-deep .el-upload-list {
  .el-upload-list__item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    background: #f8f9fa;
    border-radius: 4px;
    transition: all 0.3s;

    &:hover {
      background: #f0f7ff;
    }
  }
}

.hide {
  :deep(.el-upload--picture-card) {
    display: none;
  }
}
::v-deep .el-upload-list__item .el-icon-close {
  display: flex !important;
  top: 8px;
  right: 10px;
}
::v-deep .el-upload-list__item-status-label {
  display: none !important;
}

::v-deep .el-radio-button__inner {
  border-radius: 4px;
  margin-left: 10px;
  border: 1px solid #dcdfe6;
  -webkit-box-shadow: none;
}

::v-deep .el-radio-button .el-radio-button__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-shadow: none;
}

::v-deep .el-radio-button:focus:not(.is-focus):not(:active):not(.is-disabled) {
  box-shadow: none;
}

::v-deep .el-dialog__header {
  display: none;
}

::v-deep .el-dialog__body {
  padding: 0;
}

::v-deep .el-form-item__label {
  display: contents;
}

.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 28px 26px;
  border-bottom: 1px solid #e4e4e4;
  margin-bottom: 28px;

  div {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 24px;
    color: #333333;
  }

  i {
    font-size: 22px;
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}

::v-deep .el-upload-list--picture-card .el-upload-list__item {
  width: 135px;
  height: 135px;
  line-height: 135px;
  margin-right: 15px;
}

::v-deep .el-upload--picture-card {
  width: 135px;
  height: 135px;
  line-height: 135px;
}

::v-deep .el-upload-list--picture-card .el-upload-list__item-thumbnail {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

// 添加禁用状态的样式
::v-deep .el-radio.is-disabled {
  .el-radio__label {
    color: #c0c4cc;
  }

  .el-radio__input.is-checked .el-radio__inner {
    background-color: #c0c4cc;
    border-color: #c0c4cc;
  }
}
</style>
